package com.yra.controller.manager;

import com.yra.domain.Category;
import com.yra.domain.Product;
import com.yra.service.CategoryService;
import com.yra.service.ProductService;
import com.yra.util.PageQueryUtil;
import com.yra.util.Result;
import com.yra.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @Author: yrq
 * @Date: Create in 8:43 2019/12/4
 */
@Controller
@RequestMapping("/manager")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/products",method = RequestMethod.GET)
    public ModelAndView goToProducts(){
        ModelAndView mv = new ModelAndView("/admin/newbee_mall_goods");
        return mv;
    }

    @RequestMapping(value = "/products/list",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Result> productList(@RequestParam Map<String,Object> param){
        PageQueryUtil pageQueryUtil = new PageQueryUtil(param);
        return ResponseEntity.status(HttpStatus.OK).body(ResultGenerator.genSuccessResult(productService.productListPage(pageQueryUtil)));
    }

    @RequestMapping(value = "/products/edit",method = RequestMethod.GET)
    public ModelAndView goToProductEdit(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/admin/newbee_mall_goods_edit");
        selectCategoryList(request);
        return mv;
    }

    @RequestMapping(value = "/products/edit/{id}",method = RequestMethod.GET)
    public ModelAndView goToProductEditById(HttpServletRequest request, @PathVariable("id") int id){
        ModelAndView mv = new ModelAndView("/admin/newbee_mall_goods_edit");
        selectCategoryList(request);
        Product product = productService.productById(id);
        request.setAttribute("product",product);
        return mv;
    }

    public void selectCategoryList(HttpServletRequest request){
        List<Category> firstLevelCategories = categoryService.categoryListByLevelAndParentId(1,0);
        if (firstLevelCategories.size()>0){
            List<Category> secondLevelCategories = categoryService.categoryListByLevelAndParentId(2, firstLevelCategories.get(0).getCategoryId());
            if (secondLevelCategories.size()>0){
                List<Category> thirdLevelCategories = categoryService.categoryListByLevelAndParentId(3, secondLevelCategories.get(0).getCategoryId());
                request.setAttribute("thirdLevelCategories", thirdLevelCategories);
            }
            request.setAttribute("secondLevelCategories", secondLevelCategories);
        }
        request.setAttribute("firstLevelCategories", firstLevelCategories);
    }

    @RequestMapping(value = "/products/status/{status}",method = RequestMethod.PUT)
    public ResponseEntity<Boolean> changeProductStatus(@PathVariable("status") int status,@RequestBody int[] ids){
        categoryService.changeProductStatusById(status,ids);
        return ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @RequestMapping(value = "product/save",method = RequestMethod.POST)
    public ResponseEntity<Boolean> productInsert(@RequestBody Product product){
        try {
            System.out.println(product);
            product.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            productService.insertProduct(product);
            return ResponseEntity.status(HttpStatus.OK).body(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(false);
        }
    }

    @RequestMapping(value = "product/update",method = RequestMethod.POST)
    public ResponseEntity<Boolean> productUpdate(@RequestBody Product product){
        try {
            productService.updateProduct(product);
            return ResponseEntity.status(HttpStatus.OK).body(true);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(false);
        }
    }
}
